//version 1.0.1 
package roster;

import java.util.Scanner;
import java.io.*;

public class Controller {
	private static Schedule readScheduleResult=null;
	private static String schedulePath="data";
	
	public static String[] getAllScheduleNames(){
		File folder=new File(schedulePath);
		File[] txtList=folder.listFiles();
		int count=0;
		for(int i=0;i<txtList.length;i++){
			String tmp=txtList[i].getName();
			if(tmp.endsWith(".txt")||tmp.endsWith(".TXT"))
					count++;
		}
		String[] files=new String[count];
		for(int i=0,j=0;i<count && j<txtList.length;j++){
			String tmp=txtList[j].getName();
			if(tmp.endsWith(".txt")||tmp.endsWith(".TXT")){
					files[i]=tmp.replaceFirst("[.][^.]+$", "");
					i++;
			}
		}
		return files;
	}
	
	public static boolean deleteSchedule(Schedule obj){
		File file=new File(schedulePath+"/"+obj.getName()+".txt");
		if(!file.exists())
			return false;
		if(!file.canWrite())
			return false;
		return file.delete();
	}
	
	//read schedule data
	//return if it is successful
	//if successful you can call getReadResult() to get the schedule object
	public static boolean readSchedule(String scheduleName){
		FileReader file = null;
	
		try{
			file = new FileReader(schedulePath+"/"+scheduleName+".txt");
			Scanner input = new Scanner(file); 
			
			//insert name
			String name="";
			if(input.hasNext())
				name=input.nextLine();
			//insert date
			int date=0;
			if(input.hasNext())
				date=Integer.parseInt(input.next());
			//insert no of staffs
			int nStaffs=0;
			if(input.hasNext())
				nStaffs=Integer.parseInt(input.next());
			//insert time slot
			//3 slots only
			//String name[]=new String[nStaffs];
			//String time[]=new String[nStaffs];
			String staffName[]=new String[nStaffs];
			String time[][]=new String[nStaffs][Schedule.noOfDays];
			for(int i=0;input.hasNext() && i<nStaffs;i++) {
				staffName[i]=input.next();
				for(int j=0;j<Schedule.noOfDays;j++){
					time[i][j]=input.next();
				}
			}
			readScheduleResult = new Schedule(nStaffs, name, date, staffName, time);
		} catch(FileNotFoundException e){
			return false;
		} finally{
			if (file != null) {
				try {
					file.close();
				} catch (IOException e) {
					return false;
					}
			}
		}
		return true;
	}
	
	//get the schedule object after readData()
	public static Schedule getReadScheduleResult(){
		return readScheduleResult;
	}
	
	//receive a schedule object
	//save the data to data.txt
	//return if it is successful
	public static boolean writeSchedule(Schedule scheduleObj){	
		try {
			PrintStream file;

			file = new PrintStream(schedulePath+"/"+scheduleObj.getName()+".txt");
			file.println(scheduleObj.getName());
			file.println(scheduleObj.getStartDate());
			file.println(scheduleObj.getNoOfRecords());
			for(int i=0; i<scheduleObj.getNoOfRecords(); i++){
				file.print(scheduleObj.getStaffName()[i]);
				for(int j=0; j<Schedule.noOfDays; j++){
					file.print(" "+scheduleObj.getTime()[i][j]);
				}
				file.println();
			}
			file.close();
			return true;
			
		} catch(Exception e){
			return false;
			}	
	}
}